﻿using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Models.Common;
using Models.WebClient;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using UtilsORM;
using UtilsORM.MySqlData;

////using System.Web.Script.Serialization;

namespace ServiceWeb.DBGateway
{
    public class DBIotDeviceTaskPlainItem
    {

        EFYihongIotContext ef = new EFYihongIotContext();

        public void setPlainItemValue(string pguid, int modelno, string val)
        {
            EFYihongIotContext ef = new EFYihongIotContext();
            // iot_device_task_plain linkage = ef.iot_device_task_plain.Where(x => x.tp_p_guid == pguid).FirstOrDefault();

            iot_device_task_plain_item item = (from l in ef.iot_device_task_plain
                                               join c in ef.iot_device_task_plain_item on l.tp_guid equals c.tpi_tp_guid
                                               where l.tp_p_guid == pguid && c.tpi_p_model_no == modelno
                                               select c).FirstOrDefault();


            ef.iot_device_task_plain_item.Where(c => c.tpi_id == item.tpi_id).ExecuteUpdate(c =>
            c.SetProperty(c => c.tpi_value, val));

      
        }

        public List<iot_device_task_plain_item> getPlainItemValue(string pguid)
        {
            EFYihongIotContext ef = new EFYihongIotContext();
            //iot_device_task_plain linkage = ef.iot_device_task_plain.Where(x => x.tp_p_guid == pguid).FirstOrDefault();
            //return ef.iot_device_task_plain_item.Where(c => c.tpi_tp_guid == linkage.tp_guid).ToList();

            return (from l in ef.iot_device_task_plain
                    join c in ef.iot_device_task_plain_item on l.tp_guid equals c.tpi_tp_guid
                    where l.tp_p_guid == pguid
                    select c).ToList();
        }

        public ViewTaskPlainRunTime getPlainItemTime(string pguid, int startmodelno, int endmodelno)
        {
            EFYihongIotContext ef = new EFYihongIotContext();
            var itemStart = (from l in ef.iot_device_task_plain
                                                    join c in ef.iot_device_task_plain_item on l.tp_guid equals c.tpi_tp_guid
                                                    where l.tp_p_guid == pguid && c.tpi_p_model_no == startmodelno
                                                    select c).Select(c=>new { c.tpi_execute_time, c.tpi_complete_time }).FirstOrDefault();

            var itemEnd = (from l in ef.iot_device_task_plain
                                                  join c in ef.iot_device_task_plain_item on l.tp_guid equals c.tpi_tp_guid
                                                  join h in ef.iot_device_task_plain_item_handle on c.tpi_guid equals h.tpi_guid
                                                  where l.tp_p_guid == pguid && c.tpi_p_model_no == endmodelno
                                                  select h).Select(c => new { c.tpi_execute_delayed_time ,c.tpi_value }).FirstOrDefault();
            ViewTaskPlainRunTime itemExt = new ViewTaskPlainRunTime();

            itemExt = new ViewTaskPlainRunTime();// new UtilsClassClone().CloneByReflection<iot_device_task_plain_item, Iot_device_task_plain_itemExt>(itemStart);
            if (itemStart.tpi_execute_time != null)
            {
                itemExt.lastExecuteTime = Convert.ToDateTime(itemStart.tpi_execute_time).ToString("yyyy-MM-dd HH:mm:ss");
            }
            if (itemStart.tpi_complete_time != null && itemEnd!=null && itemEnd.tpi_execute_delayed_time!=null)
            {
                int totalSec = int.Parse(itemEnd.tpi_value);

                //  itemExt.nextExecuteTime = Convert.ToDateTime(itemEnd.tpi_execute_time).AddSeconds(double.Parse(totalSec.ToString())).ToString("yyyy-MM-dd HH:mm:ss");
                itemExt.nextExecuteTime =(itemEnd.tpi_execute_delayed_time.ToString());
                TimeSpan ts = Convert.ToDateTime(itemExt.nextExecuteTime) - DateTime.Now;
                itemExt.nextHowLongSecond = ts.TotalSeconds.ToString();
            }
            return itemExt;
        }


        public iot_device_task_plain_item getPlainItem(string guid)
        {
            return ef.iot_device_task_plain_item.Where(c => c.tpi_guid == guid).FirstOrDefault();
        }

        public List<iot_device_task_plain_item> getPlainItemList(string tpguid)
        {
            return ef.iot_device_task_plain_item.Where(c => c.tpi_tp_guid == tpguid).OrderBy(c => c.tpi_order).ToList();
        }

        public DataTable getPlainItemListTable(string tpguid)
        {
            string sql = "SELECT * from iot_device_task_plain_item left join iot_device on tpi_taskobj_guid=d_guid  inner join iot_device_command on dc_model_guid=d_model_guid and dc_key=tpi_key where tpi_tp_guid='" + tpguid + "' order by tpi_order asc";
            return MySqlYihongIot.GetDataTable(sql);
        }

        public DataTable getPlainItemTable(string tpiguid)
        {
            string sql = "SELECT * from iot_device_task_plain_item left join iot_device on tpi_taskobj_guid=d_guid  inner join iot_device_command on dc_model_guid=d_model_guid and dc_key=tpi_key where tpi_guid='" + tpiguid + "' order by tpi_order asc";
            return MySqlYihongIot.GetDataTable(sql);
        }
        public void delItem(string guid)
        {
            iot_device_task_plain_item item = ef.iot_device_task_plain_item.Where(c => c.tpi_guid == guid).FirstOrDefault();
            ef.iot_device_task_plain_item.Remove(item);
            ef.SaveChanges();
        }

        public void delItemAll(string tpguid)
        {
            ef.iot_device_task_plain_item.Where(c => c.tpi_tp_guid == tpguid).ExecuteDelete();

        }

        public bool saveAll(iot_device_task_plain_item req)
        {
            iot_device_task_plain plain = ef.iot_device_task_plain.Where(c => c.tp_guid == req.tpi_tp_guid).FirstOrDefault();

            iot_device_task_plain_item lastPlain_Item = (from l in ef.iot_device_task_plain
                                                     join c in ef.iot_device_task_plain_item on l.tp_guid equals c.tpi_tp_guid
                                                     where l.tp_p_guid == plain.tp_p_guid && c.tpi_p_model_no!=null
                                                         select c).OrderByDescending(c => c.tpi_p_model_no).FirstOrDefault();

            //先查一下有没重复提交
            iot_device_task_plain_item model = ef.iot_device_task_plain_item.Where(c => c.tpi_guid == req.tpi_guid).FirstOrDefault();
            if (model == null)
            {

                //写入
                req.tpi_add_time = DateTime.Now;
                req.tpi_mod_time = DateTime.Now;
                req.tpi_complete = 1;

                // iot_device_task_plain_item getmodel = ef.iot_device_task_plain_item.Where(c => c.tpi_tp_guid == req.tpi_tp_guid).OrderByDescending(c => c.tpi_id).FirstOrDefault();
                if (lastPlain_Item != null && lastPlain_Item.tpi_p_model_no != null)
                {
                    req.tpi_p_model_no = lastPlain_Item.tpi_p_model_no + 1;
                }
                else
                {
                    req.tpi_p_model_no = 1;
                }


                if (req.tpi_order == null)
                {
                    //排序
                    iot_device_task_plain_item itemorder = ef.iot_device_task_plain_item.Where(c => c.tpi_tp_guid == req.tpi_tp_guid).OrderByDescending(c => c.tpi_order).FirstOrDefault();
                    if (itemorder != null && itemorder.tpi_order != null)
                    {
                        req.tpi_order = itemorder.tpi_order + 1;
                    }
                    else
                    {
                        req.tpi_order = 1;
                    }
                }

                ef.iot_device_task_plain_item.Add(req);
                ef.SaveChanges();
            }
            else
            {
                model.tpi_title = req.tpi_title;
                model.tpi_status = req.tpi_status;
                model.tpi_taskobj_guid = req.tpi_taskobj_guid;
                model.tpi_type = req.tpi_type;
                model.tpi_key = req.tpi_key;
                model.tpi_value = req.tpi_value;
                model.tpi_value_rlue = req.tpi_value_rlue;

                model.tpi_order = req.tpi_order;
                model.tpi_repeat_count = req.tpi_repeat_count;
                model.tpi_repeat_interval = req.tpi_repeat_interval;
                //model.ti_repeat_count = req.ti_repeat_count;
                model.tpi_mod_time = DateTime.Now;
                ef.iot_device_task_plain_item.Update(model);
                ef.SaveChanges();
            }

            return true;
        }
        public iot_device_task_plain_item getPlainItemByModelNo(string pguid, int modelno)
        {
            EFYihongIotContext ef = new EFYihongIotContext();
            // iot_device_task_plain linkage = ef.iot_device_task_plain.Where(x => x.tp_p_guid == pguid).FirstOrDefault();

            iot_device_task_plain_item item = (from l in ef.iot_device_task_plain
                                               join c in ef.iot_device_task_plain_item on l.tp_guid equals c.tpi_tp_guid
                                               where l.tp_p_guid == pguid && c.tpi_p_model_no == modelno
                                               select c).FirstOrDefault();

            return item;
        }


    }
}
